<template>
  <j-modal
    :title="title"
    :width="1200"
    :visible="visible"
    switchFullscreen
    @ok="handleOk"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }"
    @cancel="close"
    cancelText="关闭"
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="formRef" :model="form" :label-col="labelCol" :wrapper-col="wrapperCol" class="layout-inline">
        <div class="in-box">
          <a-form-model-item :wrapper-col="{ span: 24 }">
            <model-tree ref="modelTreeRef" @checkedKeys="getCheckedKeys" @selectedKeys="getSelectedKeys" />
          </a-form-model-item>
        </div>
        <div class="in-box" v-if="selectedKeys && selectedKeys!=='9'">
          <a-form-model-item :wrapper-col="{ span: 24 }" >
            <custom-table
              ref="table"
              rowKey="phoneNumber"
              :showAlert="false"
              :showSelection="true"
              :loading="loading"
              :columns="columns"
              :dataSource="pageData"
              :pagination="{showSizeChanger: false}"
              @selectedRow="onSelectedRow"
              @change="onTableChange"
            />
          </a-form-model-item>
        </div>
        <div class="in-box" style="padding-top:10px;flex:1;" v-if="selectedKeys">
          <!--//漫入告警-->
          <template v-if="selectedKeys === '0'">
            <a-form-model-item label="进入区域">
              <a-select mode="multiple" placeholder="选择区域" v-model="entryArea.alarmAreas">
                <a-select-option v-for="(item, i) in dictOptions" :key="i" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="红色告警">
              目标进入重点区域后，停留时长超过<a-input type="number" class="input" :min="0" v-model="entryArea.stayTime" placeholder="请输入"/>分钟
            </a-form-model-item>
            <a-form-model-item label="橙色告警"> 目标进入重点区域 </a-form-model-item>
            <a-form-model-item label="黄色告警">
              目标进入靠近重点区域周边<a-input type="number" class="input" :min="0" v-model="entryArea.nearRange" placeholder="请输入"/>
              米范围内，徘徊时间<a-input type="number" class="input" :min="0" v-model="entryArea.lingerTime" placeholder="请输入"/>分钟以上
            </a-form-model-item>
          </template>
          <!--//漫出告警-->
          <template v-if="selectedKeys === '1'">
            <a-form-model-item label="离开区域">
              <a-select mode="multiple" placeholder="选择区域" v-model="leaveArea.alarmAreas">
                <a-select-option v-for="(item, i) in dictOptions" :key="i" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="红色告警"> 目标离开重点区域 </a-form-model-item>
          </template>
          <!--//特定地区重点人员布控触网-->
          <template v-if="selectedKeys === '16'">
            <a-form-model-item label="进入区域">
              <a-select mode="multiple" placeholder="选择区域" v-model="keyPerson.alarmAreas">
                <a-select-option v-for="(item, i) in dictOptions" :key="i" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="红色告警">
              每天的<a-time-picker v-model="keyPerson.beginTime" class="input time" format="HH:mm:ss" valueFormat="HH:mm:ss" placeholder="请选择"/>--
              <a-time-picker v-model="keyPerson.endTime" class="input time" format="HH:mm:ss" valueFormat="HH:mm:ss" placeholder="请选择"/>目标进入重点区域
            </a-form-model-item>
          </template>
          <!--//消失告警-->
          <template v-if="selectedKeys === '15'">
            <a-form-model-item label="红色告警">
              目标无信令位置信息超过<a-input type="number" class="input" :min="0" v-model="disappearance.red" placeholder="请输入"/>分钟
            </a-form-model-item>
            <a-form-model-item label="橙色告警">
              目标无信令位置信息超过<a-input type="number" class="input" :min="0" v-model="disappearance.orange" placeholder="请输入"/>分钟
            </a-form-model-item>
            <a-form-model-item label="黄色告警">
              目标无信令位置信息超过<a-input type="number" class="input" :min="0" v-model="disappearance.yellow" placeholder="请输入"/>分钟
            </a-form-model-item>
          </template>
          <!--//进入/离开关注海域-->
          <template v-if="selectedKeys === '0,1'">
            <a-form-model-item :wrapper-col="{ span: 19, offset: 0 }" style="margin-left: 42px;">
              <a-radio-group v-model="areaBtn" button-style="solid">
                <a-radio-button value="0"> 进入 </a-radio-button>
                <a-radio-button value="1"> 离开 </a-radio-button>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="进入区域" v-if="areaBtn==='0'">
              <a-select mode="multiple" placeholder="选择区域" v-model="entryArea.alarmAreas">
                <a-select-option v-for="(item, i) in dictOptions" :key="i" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="红色告警" v-if="areaBtn==='0'">
              目标进入重点区域后，停留时长超过<a-input type="number" class="input" :min="0" v-model="entryArea.stayTime" placeholder="请输入"/>分钟
            </a-form-model-item>
            <a-form-model-item label="橙色告警" v-if="areaBtn==='0'"> 目标进入重点区域 </a-form-model-item>
            <a-form-model-item label="黄色告警" v-if="areaBtn==='0'">
              目标进入靠近重点区域周边<a-input type="number" class="input" :min="0" v-model="entryArea.nearRange" placeholder="请输入"/>
              米范围内，徘徊时间<a-input type="number" class="input" :min="0" v-model="entryArea.lingerTime" placeholder="请输入"/>分钟以上
            </a-form-model-item>
            <a-form-model-item label="离开区域" v-if="areaBtn==='1'">
              <a-select mode="multiple" placeholder="选择区域" v-model="leaveArea.alarmAreas">
                <a-select-option v-for="(item, i) in dictOptions" :key="i" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
            <a-form-model-item label="红色告警" v-if="areaBtn==='1'"> 目标离开重点区域 </a-form-model-item>
          </template>
          <!--//走私出/入岛行为-->
          <template v-if="selectedKeys === '14'">
            <a-form-model-item :wrapper-col="{ span: 19, offset: 0 }" style="margin-left: 42px;">
              <a-radio-group v-model="accessBtn" button-style="solid">
                <a-radio-button value="0"> 入岛 </a-radio-button>
                <a-radio-button value="1"> 出岛 </a-radio-button>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item v-if="accessBtn==0" label="红色告警">目标进入岛内</a-form-model-item>
            <a-form-model-item v-if="accessBtn==1" label="红色告警">目标离开岛内</a-form-model-item>
          </template>
          <!--//走私外省人员入岛-->
          <template v-if="selectedKeys === '6'">
            <a-form-model-item label="指定归属地">
              <linkage-area type="multi_select" placeholder="请选择归属地（支持模糊搜索）" v-model="outProvicePerson.homeNameList" />
            </a-form-model-item>
            <a-form-model-item label="红色告警">
              目标对象与指定外地区域人员有通联，且24小时内该外省人员入岛时告警
            </a-form-model-item>
          </template>
          <!--//走私多人同一时间段出入境-->
          <template v-if="selectedKeys === '7,8'">
            <a-form-model-item :wrapper-col="{ span: 19, offset: 0 }" style="margin-left: 42px;">
              <a-radio-group v-model="countryBtn" button-style="solid">
                <a-radio-button value="7"> 入境 </a-radio-button>
                <a-radio-button value="8"> 出境 </a-radio-button>
              </a-radio-group>
            </a-form-model-item>
            <a-form-model-item label="红色告警" v-if="countryBtn==='7'">
              至少<a-input type="number" class="input" :min="0" v-model="enterCountry.red.peopleNum" placeholder="请输入"/>人在
              <a-input type="number" class="input" :min="0" v-model="enterCountry.red.timeNum" placeholder="请输入"/>分钟内入境
            </a-form-model-item>
            <a-form-model-item label="橙色告警" v-if="countryBtn==='7'">
              至少<a-input type="number" class="input" :min="0" v-model="enterCountry.orange.peopleNum" placeholder="请输入"/>人在
              <a-input type="number" class="input" :min="0" v-model="enterCountry.orange.timeNum" placeholder="请输入"/>分钟内入境
            </a-form-model-item>
            <a-form-model-item label="黄色告警" v-if="countryBtn==='7'">
              至少<a-input type="number" class="input" :min="0" v-model="enterCountry.yellow.peopleNum" placeholder="请输入"/>人在
              <a-input type="number" class="input" :min="0" v-model="enterCountry.yellow.timeNum" placeholder="请输入"/>分钟内入境
            </a-form-model-item>
            <a-form-model-item label="红色告警" v-if="countryBtn==='8'">
              至少<a-input type="number" class="input" :min="0" v-model="outCountry.red.peopleNum" placeholder="请输入"/>人在
              <a-input type="number" class="input" :min="0" v-model="outCountry.red.timeNum" placeholder="请输入"/>分钟内出境
            </a-form-model-item>
            <a-form-model-item label="橙色告警" v-if="countryBtn==='8'">
              至少<a-input type="number" class="input" :min="0" v-model="outCountry.orange.peopleNum" placeholder="请输入"/>人在
              <a-input type="number" class="input" :min="0" v-model="outCountry.orange.timeNum" placeholder="请输入"/>分钟内出境
            </a-form-model-item>
            <a-form-model-item label="黄色告警" v-if="countryBtn==='8'">
              至少<a-input type="number" class="input" :min="0" v-model="outCountry.yellow.peopleNum" placeholder="请输入"/>人在
              <a-input type="number" class="input" :min="0" v-model="outCountry.yellow.timeNum" placeholder="请输入"/>分钟内出境
            </a-form-model-item>
          </template>
          <!--//走私聚集-->
          <template v-if="selectedKeys === '3'">
            <a-form-model-item label="红色告警">
              满足橙色告警时，且<a-input type="number" class="input" :min="0" v-model="aggregation.time" placeholder="请输入"/>分钟内目标人群轨迹一致
            </a-form-model-item>
            <a-form-model-item label="橙色告警">
              至少<a-input type="number" class="input" :min="0" v-model="aggregation.number" placeholder="请输入"/>人且实时距离小于
              <a-input type="number" class="input" :min="0" v-model="aggregation.nearRange" placeholder="请输入"/>米
            </a-form-model-item>
          </template>
          <!--//疑似走私运输车联关联-->
          <template v-if="selectedKeys === '9'">
            <rule-map-draw ref="map" v-if="reloading" @on-transport-vehicle="getTransportVehicle" />
          </template>
          <!--//走私异常出行-->
          <template v-if="selectedKeys === '10'">
            <a-form-model-item label="红色告警">
              至少<a-input type="number" class="input" :min="0" v-model="abnormalTravel.red" placeholder="请输入"/>人在同一天乘坐相同车次或航班
            </a-form-model-item>
            <a-form-model-item label="橙色告警">
              至少<a-input type="number" class="input" :min="0" v-model="abnormalTravel.orange" placeholder="请输入"/>人在同一天乘坐相同车次或航班
            </a-form-model-item>
            <a-form-model-item label="黄色告警">
              至少<a-input type="number" class="input" :min="0" v-model="abnormalTravel.yellow" placeholder="请输入"/>人在同一天乘坐相同车次或航班
            </a-form-model-item>
          </template>
          <!--//走私外地通联-->
          <template v-if="selectedKeys === '11'">
            <a-form-model-item label="指定归属地">
              <linkage-area type="multi_select" placeholder="请选择归属地（支持模糊搜索）" v-model="outlandContact.homeNameList" />
            </a-form-model-item>
            <a-form-model-item label="红色告警">
              通联次数至少<a-input type="number" class="input" :min="0" v-model="outlandContact.red" placeholder="请输入"/>次
            </a-form-model-item>
            <a-form-model-item label="橙色告警">
              通联次数至少<a-input type="number" class="input" :min="0" v-model="outlandContact.orange" placeholder="请输入"/>次
            </a-form-model-item>
            <a-form-model-item label="黄色告警">
              通联次数至少<a-input type="number" class="input" :min="0" v-model="outlandContact.yellow" placeholder="请输入"/>次
            </a-form-model-item>
          </template>
          <!--//走私异常活跃群体-->
          <template v-if="selectedKeys === '13'">
            <a-form-model-item label="红色告警">
              在<a-input type="number" class="input" :min="0" v-model="activeGroup.red.timeNum" placeholder="请输入" />分钟内，通联次数至少
              <a-input type="number" class="input" :min="0" v-model="activeGroup.red.num" placeholder="请输入"/>次
            </a-form-model-item>
            <a-form-model-item label="橙色告警">
              在<a-input type="number" class="input" :min="0" v-model="activeGroup.orange.timeNum" placeholder="请输入"/>分钟内，通联次数至少
              <a-input type="number" class="input" :min="0" v-model="activeGroup.orange.num" placeholder="请输入"/>次
            </a-form-model-item>
            <a-form-model-item label="黄色告警">
              在<a-input type="number" class="input" :min="0" v-model="activeGroup.yellow.timeNum" placeholder="请输入"/>分钟内，通联次数至少
              <a-input type="number" class="input" :min="0" v-model="activeGroup.yellow.num" placeholder="请输入"/>次
            </a-form-model-item>
          </template>
          <!--//一人多机-->
          <template v-if="selectedKeys === '12'">
            <a-form-model-item label="一人多机红色告警">识别出目标号码一人多机</a-form-model-item>
          </template>
          <!--//换机、换卡-->
          <template v-if="selectedKeys === '4'">
            <a-form-model-item label="换卡红色告警">识别出目标号码换卡不换机</a-form-model-item>
            <a-form-model-item label="换机红色告警">识别出目标号码换机不换卡</a-form-model-item>
          </template>
          <!--//走私人群集体关机-->
          <template v-if="selectedKeys === '2'">
            <a-form-model-item label="红色告警">满足橙色告警时，且关机目标之间存在通联关系</a-form-model-item>
            <a-form-model-item label="橙色告警">
              至少<a-input type="number" class="input" :min="0" v-model="offPhone.number" placeholder="请输入"/>人出现集体关机情况<br>
              关机时段为每天的<a-time-picker v-model="offPhone.beginTime" class="input time" format="HH:mm:ss" valueFormat="HH:mm:ss" placeholder="请选择"/>--
              <a-time-picker v-model="offPhone.endTime" class="input time" format="HH:mm:ss" valueFormat="HH:mm:ss" placeholder="请选择"/>时间
            </a-form-model-item>
            <!-- <a-form-model-item :wrapper-col="{ span: 21, offset: 1 }" >
              <p style="color: red">注：在未勾选目标人或目标人不足2个人的时候，以上三个输入框属于禁用状态，且自动清空输入框内容</p>
            </a-form-model-item> -->
          </template>
          <!--//访问互联网-->
          <template v-if="selectedKeys === '5'">
            <a-form-model-item label="红色告警">访问设置的信源</a-form-model-item>
            <a-form-model-item label="信源">
              <a-select mode="tags" placeholder="请选择" style="width: 200px" v-model="internet.sources">
                <a-select-option v-for="(item, i) in sources" :key="i" :value="item.value">
                  {{ item.label }}
                </a-select-option>
              </a-select>
            </a-form-model-item>
          </template>
        </div>
      </a-form-model>
    </a-spin>
    <template slot="footer">
      <a-button key="back" @click="close">取消</a-button>
      <a-button key="submit" type="primary" @click="handleOk">确定</a-button>
    </template>
  </j-modal>
</template>

<script>
import * as eventModelApi from '@/api/wxSmuggle/eventModel.js'
import * as alarmApi from '@/api/wxSmuggle/alarm.js'
import * as caseEventApi from '@/api/wxSmuggle/caseEvent.js'
import { getDictItemsFromCache } from '@/api/api'
import tenantid from '@/utils/tenantFilter';
import modelTree from '../../modules/modelTree'
import CustomTable from '@/components/zxd/Table/CustomTable'
import ruleMapDraw from './ruleMapDraw'
import linkageArea from './linkageArea'
export default {
  name: 'ruleModal',
  components: {
    modelTree,
    CustomTable,
    ruleMapDraw,
    linkageArea
  },
  props: {
    taskId: {
      type: [String, Number]
    },
    modelId: {
      type: [String, Number]
    },
    approvalId: {
      type: [String, Number]
    }
  },
  data() {
    return {
      title: '告警规则',
      visible: false,
      disableSubmit: false,
      areaBtn: '0',
      accessBtn: '0',
      countryBtn: '7',
      form: {},
      result: {},
      dictOptions: [],
      configKeys: null,
      selectedKeys: null,
      checkeddKeys: null,
      checkeddKeysTemp: null,
      labelCol: { span: 5 },
      wrapperCol: { span: 18 },
      confirmLoading: false,
      loading: false,
      reloading: true,
      sources: getDictItemsFromCache('sources'), //信源
      columns: [
        {
          title: '手机号',
          dataIndex: 'phoneNumber',
          width: 130,
          align: 'center'
        },
        {
          title: '姓名 /代号',
          dataIndex: 'userName',
          width: 115,
          align: 'center'
        },
      ],
      pageForm: {},
      pageFilter: {
        pageNo: 1,
        pageSize: 10,
      },
      pageData: {},
      // 进入关注海域(同漫入告警)
      entryArea: {},
      // 离开关注海域(同漫出告警)
      leaveArea: {},
      // 走私出/入岛行为
      access: {},
      // 走私外省人员入岛
      outProvicePerson: {},
      // 走私多人同一时间段入境
      enterCountry: {
        people: [],
        red: {},
        orange: {},
        yellow: {}
      },
      // 走私多人同一时间段出境
      outCountry: {
        people: [],
        red: {},
        orange: {},
        yellow: {}
      },
      // 消失告警
      disappearance: {},
      // 特定地区重点人员布控触网
      keyPerson: {},
      // 走私聚集
      aggregation: {},
      // 疑似走私运输车辆关联
      transportVehicle: {},
      // 走私异常出行
      abnormalTravel: {},
      // 走私外地通联
      outlandContact: {},
      // 走私异常活跃群体
      activeGroup: {
        people: [],
        red: {},
        orange: {},
        yellow: {}
      },
      // 一人多机
      multiple: {},
      // 换机、换卡
      equipment: {},
      // 走私人群集体关机
      offPhone: {},
      // 访问互联网
      internet: {},
      alarmTypeKeys: [
        {alarmTypeName: '进入/离开关注海域（进入）', alarmTypeValue: '0', keyName: 'entryArea'}, // 同漫入告警
        {alarmTypeName: '进入/离开关注海域（离开）', alarmTypeValue: '1', keyName: 'leaveArea'}, // 同漫出告警
        {alarmTypeName: '走私出/入岛行为', alarmTypeValue: '14', keyName: 'access'},//
        {alarmTypeName: '走私外省人员入岛', alarmTypeValue: '6', keyName: 'outProvicePerson'},
        {alarmTypeName: '走私多人同一时间段出入境（入境）', alarmTypeValue: '7', keyName: 'enterCountry'},
        {alarmTypeName: '走私多人同一时间段出入境（出境）', alarmTypeValue: '8', keyName: 'outCountry'},
        {alarmTypeName: '走私聚集', alarmTypeValue: '3', keyName: 'aggregation'},
        {alarmTypeName: '疑似走私运输车辆关联挖掘', alarmTypeValue: '9', keyName: 'transportVehicle'},
        {alarmTypeName: '走私异常出行', alarmTypeValue: '10', keyName: 'abnormalTravel'},
        {alarmTypeName: '走私外地通联', alarmTypeValue: '11', keyName: 'outlandContact'},
        {alarmTypeName: '走私异常活跃群体', alarmTypeValue: '13', keyName: 'activeGroup'},
        {alarmTypeName: '一人多机', alarmTypeValue: '12', keyName: 'multiple'},//
        {alarmTypeName: '换机、换卡', alarmTypeValue: '4', keyName: 'equipment'},
        {alarmTypeName: '走私人群集体关机', alarmTypeValue: '2', keyName: 'offPhone'},
        {alarmTypeName: '访问互联网', alarmTypeValue: '5', keyName: 'internet'},
        {alarmTypeName: '消失告警', alarmTypeValue: '15', keyName: 'disappearance'},
        {alarmTypeName: '特定地区重点人员布控触网', alarmTypeValue: '16', keyName: 'keyPerson'},
      ]
    }
  },
  watch: {
    selectedKeys(value) {
      let that = this
      let keyName = that.getKeyName(value)
      let keyBool = typeof that[keyName[0]] == 'object' && that[keyName[0]]!==null
      if(keyName.length>0 && keyBool && Object.keys(that[keyName[0]]).length>0 && that[keyName[0]].people && Array.isArray(that[keyName[0]].people)) {
        that.$nextTick(()=>{
          let phones = that[keyName[0]].people.map(v=>v.phone)
          if(that.$refs.table) that.$refs.table.selectedRowKeys = phones
        })
      } else {
        that.$nextTick(()=>{
          if(that.$refs.table) that.$refs.table.selectedRowKeys = []
        })
      }
    }
  },
  methods: {
    add() {
      this.visible = true
      this.$nextTick(() => {
        this.refreshTable()
        this.getPageListByUnit()
        this.getConfigKey()
      })
    },
    edit(record) {
      this.visible = true
      this.$nextTick(() => {})
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    // 提交保存
    handleOk() {
      this.$refs.formRef.validate(valid => {
        if (valid) {
          let api, form = {}
          if (this.configKeys) { //更新配置
            form = { configKey: this.configKeys, taskId: this.taskId, approvalId:this.approvalId, ...this.form }
            api = alarmApi.alarmUpdate(form)
          } else { //创建配置
            form = { rules: [this.form], taskId: this.taskId, approvalId:this.approvalId }
            api = alarmApi.alarmCreate(form)
          }
          api.then(res => {
            if (res.success) {
              let message = res.message? res.message: '操作成功'
              this.$message.success(message)
              this.visible = false
              this.$emit('ok')
            } else {
              this.$message.warn(res.message)
            }
          })
          .catch(e => {
            this.$message.warn('操作失败！')
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 目标人群列表切换分页
    onTableChange(pagination, pageFilter, sorter) {
      let keyMap = {currentPage: 'pageNo'}
      let paginations= Object.keys(pagination).reduce((newData, key) => {
        let newKey = keyMap[key] || key
        newData[newKey] = pagination[key]
        return newData
      }, {})
      this.pageFilter = Object.assign({}, this.pageFilter, paginations);
      this.refreshTable();
    },
    // 获取目标人群列表
    refreshTable() {
      this.$set(this.pageForm, 'taskId', this.taskId)
      this.$set(this.pageForm, 'modelId', this.modelId)
      let pageForm = Object.assign({}, this.pageForm, this.pageFilter)
      this.loading = true
      eventModelApi.getModelTaskUser(pageForm).then((res) => {
        if (res.success) {
          this.pageData = res.result
          this.loading = false
        } else {
          this.loading = false
          this.$message.warn(res.message);
        }
      }).catch(e=>{
        this.loading = false
        this.$message.warn("请求失败！");
      })
    },
    // 查询告警配置configKey
    async getConfigKey() {
      alarmApi.getConfigKey({taskId: this.taskId}).then((res) => {
        if (res.success) {
          this.configKeys = res.result;
          if(this.configKeys) this.getAlarmItem()
        } else {
          this.$message.warn(res.message);
        }
      }).catch(e=>{
        this.$message.warn("请求失败！");
      })
    },
    // 获取配置明细数据
    async getAlarmItem() {
      this.confirmLoading = true
      try {
        let res = await alarmApi.getAlarmItem({configKey: this.configKeys})
        let keyName = this.getKeyName(this.selectedKeys).length>0?this.getKeyName(this.selectedKeys)[0]:null
        if (res.success) {
          this.result = res.result
          let data = []
          for (const key in this.result) {
            let keyBool1 = typeof this.result[key] == 'object' && this.result[key]!==null
            let keyBool2 = typeof this.result[keyName] == 'object' && this.result[keyName]!==null
            if (key==='activeGroup' && this.result[key] && keyBool1 && Object.keys(this.result[key]).length>0) {
              if(this.result[key].red && typeof this.result[key].red =='object') this[key].red = this.result[key].red
              if(this.result[key].orange && typeof this.result[key].orange =='object') this[key].orange = this.result[key].orange
              if(this.result[key].yellow && typeof this.result[key].yellow ==='object') this[key].yellow = this.result[key].yellow
              if(this.result[key].people && Array.isArray(this.result[key].people)) this[key].people = this.result[key].people
              this.$set(this.form, key, this[key])
            } else if (key==='outCountry' && this.result[key] && keyBool1 && Object.keys(this.result[key]).length>0) {
              if(this.result[key].red && typeof this.result[key].red =='object') this[key].red = this.result[key].red
              if(this.result[key].orange && typeof this.result[key].orange =='object') this[key].orange = this.result[key].orange
              if(this.result[key].yellow && typeof this.result[key].yellow ==='object') this[key].yellow = this.result[key].yellow
              if(this.result[key].people && Array.isArray(this.result[key].people)) this[key].people = this.result[key].people
              this.$set(this.form, key, this[key])
            } else {
              if(this.result[key] && keyBool1) {
                this[key] = this.result[key]
                this.$set(this.form, key, this.result[key])
              }
            }
            if(keyBool1) data.push(key)
            if(keyName===key && keyBool2 && this.result[keyName].people && Array.isArray(this.result[keyName].people)) {
              this.$nextTick(()=>{
                let phones = this.result[key].people.map(v=>v.phone)
                // console.log(key, phones)
                this.$refs.table.selectedRowKeys = phones
              })
            }
          }
          let checkedKeys = this.getAlarmTypeValue(data)
          // console.log(checkedKeys)
          if(tenantid!==3) {
            if(checkedKeys.includes('0') || checkedKeys.includes('1')) {
              checkedKeys = [...checkedKeys, ...['0,1']]
              checkedKeys = checkedKeys.filter(item=>item!=='1' && item!=='0')
            } 
            if(checkedKeys.includes('7') || checkedKeys.includes('8')) {
              checkedKeys = [...checkedKeys, ...['7,8']]
              checkedKeys = checkedKeys.filter(item=>item!=='7' && item!=='8')
            }
            // console.log(checkedKeys)
          }
          this.$refs.modelTreeRef.checkedKeys = checkedKeys
          this.confirmLoading = false
        } else {
          this.$message.warn(res.message)
          this.confirmLoading = false
        }
      } catch (err) {
        this.$message.warn('请求失败！')
        this.confirmLoading = false
      }
    },
    // 获取区域列表
    getPageListByUnit() {
      caseEventApi
        .getPageListByUnit({ state: 1, pageNo: 1, pageSize: 99999 })
        .then(res => {
          if (res.success) {
            let result = res.result.records
            this.dictOptions = result.map(v=>{
              return {
                label: v.name,
                value: v.id,
                // count: v.count,
                // type: v.type
              }
            })
          } else {
            this.$message.warn(res.message)
          }
        })
        .catch(e => {
          this.$message.warn('请求失败！')
        })
    },
    // 选择目标人群列表
    onSelectedRow(datas) {
      console.log(datas)
      if(!datas || datas.length===0) return
      let keyName = this.getKeyName(this.selectedKeys)
      let dataList = datas.map((v) => {
        return {
          phone: v.phoneNumber,
          codeName: v.userName
        }
      }).filter((obj) => obj !== null && obj !== undefined && obj !== '')
      for (const key of keyName) {
        this.$set(this[key], 'people', dataList)
        // console.log(key, this[key])
      }
    },
    // 获取疑似走私运输车辆关联轨迹点
    getTransportVehicle(datas) {
      this.$set(this.transportVehicle, 'trackPointList', datas)
      console.log(this.transportVehicle)
    },
    // 勾选
    getCheckedKeys(keys, checkedNode) {
      // console.log(keys, checkedNode)
      let key = checkedNode.key
      this.checkeddKeys = Array.isArray(keys) ? keys.join(',') : keys
      let keyName = this.getKeyName(key)
      if (checkedNode.checked) {
        for (const key of keyName) {
          this.$set(this.form, key, this[key])
        }
      } else {
        this.form = this.filterKey(this.form, keyName)
      }
      // console.log(this.form)
    },
    // 单选
    getSelectedKeys(keys) {
      this.selectedKeys = Array.isArray(keys) ? keys.join(',') : keys
      // console.log(this.selectedKeys)
      if(this.selectedKeys==='9') {
        this.$nextTick(()=>{
          let trackPointList = Object.keys(this.transportVehicle).length>0 && this.transportVehicle.trackPointList?this.transportVehicle.trackPointList:[]
          console.log(trackPointList)
          if(this.$refs.map) this.$refs.map.onDetail(trackPointList)
        })
      }
    },
    filterKey(raw, allowed) {
      const filtered = Object.keys(raw)
      .filter(key => !allowed.includes(key))
      .reduce((obj, key) => {
        obj[key] = raw[key];
        return obj;
      }, {})
      return filtered
    },
    getKeyName(key) {
      let keys = key? key.split(','):[]
      // 取两数组的交集
      let col = [...this.alarmTypeKeys].filter(x => [...keys].some(y => y === x.alarmTypeValue))
      let keyName = col.map(v=>v.keyName)
      return keyName
    },
    getAlarmTypeValue(keys) {
      // let keys = key? key.split(','):[]
      // 取两数组的交集
      let col = [...this.alarmTypeKeys].filter(x => [...keys].some(y => y === x.keyName))
      let keyName = col.map(v=>v.alarmTypeValue)
      return keyName
    },
    /** 重新加载组件 */
    reload() {
      this.reloading = false
      this.$nextTick(() => (this.reloading = true))
    }
  }
}
</script>
<style lang="less" scoped>
.input {
  width: 90px;
  margin: 0 6px;
}
.select {
  width: 120px;
  margin: 0 10px;
}
.layout-inline {
  display: flex;
  .in-box {
    flex: 1;
    &:nth-of-type(1) {
      flex:0 0 250px;
      border-right: 1px solid #e8e8e8;
      padding-left: 10px;
      padding-right: 10px;
    }
    &:nth-of-type(2) {
      flex:0 0 290px;
      // border-right: 1px solid #e8e8e8;
    }
  }
}
/deep/.ant-table-thead > tr > th,
/deep/.ant-table-tbody > tr > td {
  padding-left: 0;
  padding-right: 0;
}
/deep/.ant-table-thead > tr:first-child > th:first-child,
/deep/.ant-table-tbody > tr > td:first-child {
  padding-left: 10px;
}
/deep/.ant-modal-body {
  padding: 0;
}
</style>